﻿//给QMS集件中间表
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:Wangjh
//create Date:2022-12-07
//Rewriter:
//Rewrite Date:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using UFC.Common;
using UFC.DBUtility;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;

namespace MESQualityHistoryRecord
{
    class QMSIssueMaterialInfo
    {
        public void Excute() {
            System.Threading.Thread thIssueMaterial = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                try
                {
                    SaveIssueMaterialData();
                }
                catch (Exception e)
                {
                    LogHelp.WriteError("集件信息中间表T_CollectMaterial", e);
                }
            }));
            thIssueMaterial.Start();








            //线程等待区域
            //机加检验检测项数据处理
            do
            {
                System.Threading.Thread.Sleep(500);
            } while (thIssueMaterial.ThreadState != System.Threading.ThreadState.Stopped);

        }

        /// <summary>
        /// 获取需要处理的集件数据
        /// </summary>
        /// <returns></returns>
        DataTable GetIssueMaterialData() {
            string strsql = @"select tc.id,cci.mfgordername,cci.containername,ws.workflowstepname BigSpec,ep.machineno EngineNo,cci.productname,cci.realcontainerno ProductBatchNo
,cci.workflowname||':'||cci.workflowrevision WorkflowBaseName,cl.productname ChildProductName,cl.productbatchno ChildProductContainerName,
decode(pi.parttype,0,'大件',1,'小件','其他') ProductType,il.issueqty Qty,e.fullname ConfirmEmpoyeeName,
to_char(il.createdate,'yyyy/mm/dd hh24:mi:ss') ConfirmDate,il.issueerpmaterialid from issueerpmaterial il 
left join enginepartplan ep on ep.containerid=il.containerid
left join containercurrentinfo cci on cci.containerid=il.containerid
left join workflowstep ws on ws.workflowstepid=il.workflowstepid
left join collecterpmaterial cl on cl.collecterpmaterialid=il.collecterpmaterialid
left join partmenuinfo pi on pi.partmenuinfoid=il.partmenuinfoid
left join employee e on e.employeeid=il.employeeid
left join T_CollectMaterial tc on tc.issueerpmaterialid=il.issueerpmaterialid
where il.createdate>=sysdate-365 and tc.issueerpmaterialid is null and rownum<=1001 --cci.mfgordername='NewMfgorder2022'
order by il.createdate desc";
            return OracleHelper.Query(strsql).Tables[0];
        }

        /// <summary>
        /// 保存集件数据
        /// </summary>
        void SaveIssueMaterialData() {
            DataTable dt = GetIssueMaterialData();
            var sqlList = new ArrayList();
            var inertSql = @"insert into T_CollectMaterial t (t.id,CreateDate,MfgOrderName,ContainerName,BigSpec,EngineNo,ProductName,ProductBatchNo,
WorkflowBaseName,ChildProductName,ChildProductContainerName,ProductType,Qty,ConfirmEmpoyeeName,ConfirmDate,IssueERPMaterialID,STATUS) values (sys_guid(),sysdate,'{0}','{1}','{2}','{3}','{4}',
'{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}',0)";
            var updateSql = @"update T_CollectMaterial tr set tr.ProductName='{0}',tr.ProductBatchNo='{1}',tr.WorkflowBaseName='{2}',tr.ChildProductName='{3}',
tr.ChildProductContainerName='{4}',tr.ProductType='{5}',Qty='{6}',ConfirmEmpoyeeName='{7}',ConfirmDate='{8}',tr.status=0 where tr.id='{9}'";
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(dr["id"].ToString()))
                {
                    sqlList.Add(string.Format(inertSql, dr["MfgOrderName"].ToString(), dr["ContainerName"].ToString(), dr["BigSpec"].ToString(), dr["EngineNo"].ToString(),
                        dr["ProductName"].ToString(), dr["ProductBatchNo"].ToString(), dr["WorkflowBaseName"].ToString(), dr["ChildProductName"].ToString(), dr["ChildProductContainerName"].ToString(), dr["ProductType"].ToString(),
                        dr["Qty"].ToString(), dr["ConfirmEmpoyeeName"].ToString(), dr["ConfirmDate"].ToString(), dr["IssueERPMaterialID"].ToString()));
                }
                else
                {
                    sqlList.Add(string.Format(updateSql, dr["ProductName"].ToString(), dr["ProductBatchNo"].ToString(), dr["WorkflowBaseName"].ToString(),
                        dr["ChildProductName"].ToString(), dr["ChildProductContainerName"].ToString(), dr["ProductType"].ToString(), dr["Qty"].ToString(),
                        dr["ConfirmEmpoyeeName"].ToString(), dr["ConfirmDate"].ToString(), dr["id"].ToString()));
                }
            }
            OracleHelper.ExecuteSqlTran(sqlList);
            LogHelp.WriteLog("QMS中间表T_CollectMaterial更新或插入数据条数：" + dt.Rows.Count.ToString());
        }

    }
}
